import Vue from 'vue'
import VueRouter from 'vue-router'
import home from './routes/home'
import mine from './routes/mine'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  ...home,
  ...mine,
  // 重定向
  {
    path:'/',
    redirect:'/home'
  }
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

// 全局导航守卫
router.beforeEach((to,from,next)=>{
    
  // 设置不需要验证的路由规则
  if (to.meta.allow) {
   next()
 } else {
   // 需要验证，获取token值，有token表示，登录过
   if (store.state.token !== '') {
     next()
   }else {
     let token = sessionStorage.getItem('token') || ''
     if (token !== '') {
       store.commit("loginsuccess", token);
       next()
     } else {
       next('/login')
     }
   }
 }
})

export default router
